# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import logging
import random
import re
from bs4 import BeautifulSoup
from selenium.webdriver import ActionChains
import urllib
import ssl
from gap_position import SlideCrack
import datetime
import json
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import subprocess
import os
from toutiao_read_excel import read_excel
from insert_sql import insert_etl_db


ssl._create_default_https_context = ssl._create_unverified_context

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')


class crawl_toutiao:

    def __init__(self):

        # 头条登录页面
        # self.login_url = "https://e.oceanengine.com/account/page/login/account?from=https%3A%2F%2Fe.oceanengine.com%2Faccount%2Fpage%2Fhome%3Flocale%3Dzh"
        self.login_url = "https://e.oceanengine.com/account/page/login/account?locale=zh"
        self.tiaoshu_url = "https://e.oceanengine.com/account/page/home?cc_account_id=2717594251169420"
        self.today_date = datetime.datetime.now().strftime('%Y-%m-%d')
        self.last_day = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
        self.dowload_file = self.last_day+"_file.xlsx"
        self.toutiao_download_file_dir = "toutiao_download_file_dir/" + self.last_day
        self.name = "user"
        self.password = "pwd"
        # self.driver = webdriver.PhantomJS(executable_path=r'/home/postgres/haoyin/phantomjs_dir/phantomjs-2.1.1-linux-x86_64/bin/phantomjs')
        self.chrome_options = webdriver.ChromeOptions()
        self.chrome_options.add_argument('--headless')
        self.chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
        # self.chrome_options.add_experimental_option('excludeSwitches', ['enable-debug'])
        # self.chrome_options.add_argument('log-level=3')
        # self.chrome_options.add_argument('Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;')
        self.driver = webdriver.Chrome(executable_path="/bin/chromedriver",
                                       chrome_options=self.chrome_options)
        self.toutiao_pic_dir = "./toutiao_pic_dir/"
        self.expense_forms_url = "https://e.oceanengine.com/account/page/fund/flow/income_expanditure?cc_account_id=2717594251169420"

    def login(self):

        self.driver.get(self.login_url)
        time.sleep(round(random.uniform(0.71, 1.59), 1))
        # 邮箱
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="account-sdk"]/section/div[3]/div[1]/div/div/input')).send_keys("%s" % self.name)

        time.sleep(round(random.uniform(0.11, 0.29), 1))
        # 密码
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="account-sdk"]/section/div[3]/div[2]/div/div/input')).send_keys("%s" % self.password)

        time.sleep(round(random.uniform(0.11, 0.69), 1))
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="account-sdk"]/section/div[3]/div[2]/div/div/input')).clear()
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="account-sdk"]/section/div[3]/div[2]/div/div/input')).send_keys("%s" % self.password)
        time.sleep(round(random.uniform(0.11, 0.69), 1))
        # 登陆
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="account-sdk"]/section/div[6]/button')).click()
        time.sleep(round(random.uniform(0.51, 1.39), 1))
        # content = self.driver.page_source

        # soup = BeautifulSoup(content, "html5lib")
        # items = soup.find('div', class_='validate-main').find_all('img')
        # i = 1
        # for single in items:
        # print(single.attrs['src'])
        #   urllib.request.urlretrieve(single.attrs['src'], filename=self.toutiao_pic_dir + 'tmp%s.png' % str(i))
        #   i += i
        try:
            back_pic = WebDriverWait(self.driver, 5).until(
                lambda driver: self.driver.find_element_by_xpath(
                    '//*[@id="account-sdk-slide-container"]/div/div[2]/img[1]')).get_attribute("src")
            urllib.request.urlretrieve(back_pic, self.toutiao_pic_dir + "tmp1.png")
            little_pic = WebDriverWait(self.driver, 5).until(
                lambda driver: self.driver.find_element_by_xpath(
                    '//*[@id="account-sdk-slide-container"]/div/div[2]/img[2]')).get_attribute("src")
            urllib.request.urlretrieve(little_pic, self.toutiao_pic_dir + "tmp2.png")

            self.step_one_by_one(self.toutiao_pic_dir + "tmp1.png", self.toutiao_pic_dir + "tmp2.png",
                                 self.toutiao_pic_dir + "result.png")
        except:
            pass

    def exec_login_crawl(self):

        try:
            self.login()
        except Exception as e:
            print (e)
        try:
            time.sleep(round(random.uniform(5.71, 7.59), 1))
            self.offical_crawl_expense()
        finally:
            pass
            # self.driver.quit()

    def get_tracks(self, distance):
        '''
        拿到移动轨迹，模仿人的滑动行为，先匀加速后匀减速
        匀变速运动基本公式：
        ①v=v0+at
        ②s=v0t+½at²
        ③v²-v0²=2as

        :param distance: 需要移动的距离
        :return: 存放每0.3秒移动的距离
        '''
        # 初速度
        v = 0
        # 单位时间为0.2s来统计轨迹，轨迹即0.2内的位移
        t = 0.24
        # 位移/轨迹列表，列表内的一个元素代表0.2s的位移
        tracks = []
        # 当前的位移
        current = 0
        # 到达mid值开始减速
        mid = distance * 3 / 5

        while current < distance:
            if current < mid:
                # 加速度越小，单位时间的位移越小,模拟的轨迹就越多越详细
                a = 2
            else:
                a = -3

            # 初速度
            v0 = v
            # 0.2秒时间内的位移
            s = v0 * t + 0.5 * a * (t ** 2)
            # 当前的位置
            current += s
            # 添加到轨迹列表
            tracks.append(round(s))

            # 速度已经达到v,该速度作为下次的初速度
            v = v0 + a * t
        return tracks

    # 滑动滑块
    def slide_steps(self, tracks):
        # '//*[@id="account-sdk-slide-container"]/div/div[3]/div[2]/div[2]'
        # '//*[@id="account-sdk-slide-container"]/div/div[3]/div[2]/div[2]/div/*[name()="svg"]'
        button = WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="account-sdk-slide-container"]/div/div[3]/div[2]/div[2]'))
        ActionChains(self.driver).click_and_hold(button).perform()
        # 滑动
        for track in tracks:
            ActionChains(self.driver).move_by_offset(xoffset=track, yoffset=0).perform()

        # 停顿一下
        time.sleep(round(random.uniform(0.11, 0.59), 1))

        # 小范围震荡一下
        random_num = random.randint(1, 3)
        ActionChains(self.driver).move_by_offset(xoffset=-random_num, yoffset=0).perform()
        ActionChains(self.driver).move_by_offset(xoffset=random_num, yoffset=0).perform()
        time.sleep(round(random.uniform(0.31, 0.89), 1))
        ActionChains(self.driver).release().perform()
        time.sleep(round(random.uniform(5.31, 6.89), 1))

    def step_one_by_one(self, image1, image2, image3):

        distance = SlideCrack(image1, image2, image3).discern()
        print(distance)
        # 模拟人的行为习惯，根据总位移得到行为轨迹
        tracks = self.get_tracks(distance + 40)

        # 按照行动轨迹先正向滑动，后反滑动
        try:
            self.slide_steps(tracks)

        except Exception as e:
            print (e)

    def judge_dir(self, need_check_dir):

        # 判断前一天日志存储目录是否存在，不存在创建
        if not os.path.exists(need_check_dir):
            os.makedirs(need_check_dir)

        else:
            logging.info(need_check_dir + " already exists !")

    def offical_crawl_expense(self):

        retry_num = 1
        while True:
            self.driver.get(self.expense_forms_url)
            time.sleep(round(random.uniform(3.71, 4.59), 1))
            if self.driver.current_url != self.expense_forms_url:
                time.sleep(round(random.uniform(3.71, 4.59), 1))
                self.login()
                print(self.driver.current_url)
                retry_num += 1
                if retry_num == 13:
                    break
            else:
                break
        # print(self.driver.page_source)
        self.driver.get(self.expense_forms_url)
        time.sleep(round(random.uniform(2.71, 3.59), 1))
        self.driver.execute_script("window.scrollTo(500,0)")
        time.sleep(round(random.uniform(2.71, 3.59), 1))
        # WebDriverWait(self.driver, 5).until(
        #     lambda driver: self.driver.find_element_by_xpath(
        #         '//*[@id="app"]/div/div/div[1]/div[1]/div[2]/div/input')).send_keys("多彩")
        #
        # #查询
        # WebDriverWait(self.driver, 5).until(
        #     lambda driver: self.driver.find_element_by_xpath(
        #         '//*[@id="app"]/div/div/div[1]/div[2]/button[1]')).click()
        # time.sleep(round(random.uniform(2.71, 3.59), 1))
        # 下载表格
        self.driver.switch_to_default_content()
        try:
            iframe = WebDriverWait(self.driver, 7).until(
                lambda driver: self.driver.find_element_by_xpath(
                    '//*[@id="fund"]/div/iframe'))
            self.driver.switch_to_frame(iframe)
            time.sleep(round(random.uniform(2.71, 3.59), 1))

        except Exception as e:
            print (e)

        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="app"]/div/div/div[2]/div/div[1]/div[2]/button')).click()
        time.sleep(round(random.uniform(4.71, 5.59), 1))
        self.judge_dir(self.toutiao_download_file_dir)
        subprocess.call("mv -f 度小满保险* {}/{}".format(self.toutiao_download_file_dir, self.dowload_file), shell=True)
        if read_excel(self.toutiao_download_file_dir+"/"+self.dowload_file).exec_main():
            insert_etl_db(str(read_excel(self.toutiao_download_file_dir+"/"+self.dowload_file).exec_main()), 'toufang_toutiao', 'duocai').insert_mysql()


if __name__ == '__main__':
    ct = crawl_toutiao()
    ct.exec_login_crawl()
# with ProcessPoolExecutor(4) as p:
# p.submit(crawl_toutiao().exec_login_crawl)
# p.submit(crawl_baidu().exec_login_crawl)

